package com.cuit.controller.adminController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cuit.mapper.Admin.AdminMapper;
import com.cuit.pojo.Admin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@Api(tags = "后台管理")
public class adminController {
    @Resource
    private AdminMapper adminMapper;
    @RequestMapping({"admin/index","/","/index"})
    @ApiOperation("后台首页")
    public String testIndex(ModelMap modelMap) {
        System.out.println("这是后台首页");
        return "admin/index";
    }

    @RequestMapping("admin/test")
    @ApiOperation("后台测试页")
    public String test(ModelMap modelMap) {
        System.out.println("这是后台首页");
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        List<Admin> list = adminMapper.selectList(queryWrapper);
        list.forEach(System.out::println);
        modelMap.put("list",list);
        return "demo/testTwo";
    }


    @RequestMapping("/toLogin")
    @ApiOperation("shiro拦截、去登录页")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("/login")
    public String login(String username, String password, Model model){
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户的登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try{
            subject.login(token); //执行登录的方法，如果没有异常就说明ok了

            return "admin/index";
        }catch (UnknownAccountException e){ //用户名不存在
            model.addAttribute("errMsg","用户名错误");
            return "login";
        }catch (IncorrectCredentialsException e){  //密码错误
            model.addAttribute("errMsg","密码错误");
            return "login";
        }
    }

    @RequestMapping("/logout")
    @ApiOperation("管理员退出")
    public String logout(){
        return "login";
    }

    @RequestMapping("/noauth")
    @ApiOperation("权限不够的提示")
    @ResponseBody
    public String unauthorized(){
        return "未经授权无法访问此页面";
    }
    @RequestMapping("admin/admin")
    @ApiOperation("管理管理员的页面")
    public String selectAll(ModelMap modelMap){
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        List<Admin> list = adminMapper.selectList(queryWrapper);
        list.forEach(System.out::println);
        modelMap.put("list",list);
        return "admin/dashboard-project";
    }
    //删除管理员
    @RequestMapping("admin/delete/{id}")
    @ApiOperation("删除管理员")
    public String deleteOne(@PathVariable Integer id){
        System.out.println(id+"-=-==");
        adminMapper.deleteById(id);
        return "redirect:/admin/admin";
    }
    //按照id查询管理员
    @RequestMapping("admin/SearchById")
    @ApiOperation("查询管理员")
    public String SearchById(HttpServletRequest request,ModelMap modelMap){
        String  id=request.getParameter("id");
        if (id==null||id.isEmpty()){
            QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
            List<Admin> list = adminMapper.selectList(queryWrapper);
            modelMap.put("list",list);
        }else {
            Admin list=adminMapper.selectById(id);
            modelMap.put("list",list);
        }
        return "/admin/dashboard-project";
    }
    //修改管理员信息
    @RequestMapping("admin/edit")
    @ApiOperation("修改管理员信息")
    public String edit(HttpServletRequest request){
        String id = request.getParameter("id");
        String type=request.getParameter("type");
        String num=request.getParameter("num");
        String status=request.getParameter("status");
        UpdateWrapper<Admin> UpdateWrapper = new UpdateWrapper<>();
        UpdateWrapper.eq("id",id)
                .set("type",type)
                .set("num",num)
                .set("status",status);
        adminMapper.update(null,UpdateWrapper);
        return "redirect:/admin/admin";
    }
    //添加管理员
    @RequestMapping("admin/add")
    @ApiOperation("添加管理员")
    public String add(HttpServletRequest request){
        String name = request.getParameter("name1");
        String pw=request.getParameter("pw");
        String type=request.getParameter("type1");
        String num=request.getParameter("num1");
        String status=request.getParameter("status1");
        String email=request.getParameter("email");
        Admin admin=new Admin();
        admin.setType(type);
        admin.setStatus(status);
        admin.setEmail(email);
        admin.setNum(num);
        admin.setAdminname(name);
        admin.setAdminpw(pw);
        adminMapper.insert(admin);
        return "redirect:/admin/admin";
    }
}
